//Author: xiaruize
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, b, a) for (int i = (b); i >= (a); i--)
int testcase = 1, caseid;
const int N = 2e5 + 10;
const int M = 1e6 + 10;
const int INF = 0x3f3f3f3f3f3f3f3f;

void chkmx(int &x, int y)
{
    if (x < y)
        x = y;
}
int n;
int a[N];
int sum[N];
int dp[N];
int la[M];

int calc(int l, int r)
{
    if (l > r)
        return 0;
    return sum[r] - sum[l];
}

void solve()
{
    memset(dp, 0, sizeof(dp));
    cin >> n;
    rep(i, 1, n) cin >> a[i];
    rep(i, 2, n) sum[i] = sum[i - 1] + (a[i] == a[i - 1]) * a[i];
    a[n + 1] = INF;
    int lim = 0;
    if (n > 20000)
    {
        lim = 200;
    }
    rep(i, 1, n)
    {
        if (la[a[i]])
        {
            chkmx(dp[i], dp[la[a[i]] - 1] + calc(la[a[i]] + 1, i - 1) + a[i]);
        }
        rep(j, (lim ? max(1ll, i - lim + 1) : 1), i)
        {
            chkmx(dp[i], dp[j - 1] + calc(j, i) + (a[i + 1] == a[j - 1]) * a[i + 1]);
        }
    }
    cout << dp[n] << endl;
}

signed main()
{
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> testcase;
    while (testcase--)
        solve();
    return 0;
}
//g++ color.cpp -o color -std=c++14 -Wall -fsanitize=address,undefined -O2